package com.xiaohudie.controller;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.sun.deploy.net.HttpResponse;
import com.xiaohudie.pojo.AjaxResult;
import com.xiaohudie.pojo.Car;
import com.xiaohudie.pojo.Teacher;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.http.HttpRequest;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import java.util.UUID;

/**
 * @Author xiao hu die
 * @Date 2022/2/22 20:50
 * @Version 1.0
 */
/**Controller 与
 * RequestBody 的合体*/
@RestController
@RequestMapping("/hello")
@Slf4j
public class HelloController {
    @Autowired
    private StringRedisTemplate stringRedisTemplate;
    @Autowired
    private RedisTemplate<String,Object> redisTemplate;
    @Autowired
    private Car car;
    @Resource
    private Teacher teacher;
    /**对象序列化工具*/
    private  static final ObjectMapper mapper =new ObjectMapper();
    @GetMapping("/car")
    public String car(){
       // log.info("没出错!");
        String a =null;
        a.equals("1");
        return car.toString();
    }
       /**测试方法*/
       @GetMapping("/test")
       public String test(){
         //  log.error("出错了");
           int i =10/0;
         return "test";
       }
       @RequestMapping("/teacher")
       public Teacher teacher(){
         //  log.warn("出大问题了");

           return teacher;
       }
       /**Rest风格*/
       @GetMapping("/user")
       public String user(){
           return "GET-用户";
       }
       @PostMapping("/user")
       public String user1(){
           return "POST-用户";
       }
       @DeleteMapping("/user")
      public String user2(){
      return "DELETE-用户";
       }
       @PutMapping("/user")
       public String user3(/*HttpRequest httpRequest, HttpResponse httpResponse*/){
        /*   String methodValue = httpRequest.getMethodValue();
           httpResponse.getInputStream()*/
           return "PUT-用户";
       }
       @GetMapping("/redis")
    public void redis(){
           redisTemplate.opsForValue().set("name","aa");
           String name = (String) redisTemplate.opsForValue().get("name");
           System.out.println(name);
       }
       @GetMapping("/redis2")
    public void redis2(){
           redisTemplate.opsForValue().set("name:100",car);
           Car car = (Car) redisTemplate.opsForValue().get("name:100");
           System.out.println(car);
       }
       /**全部存放String，然后序列化反序列化*/
       @GetMapping("/redis3")
    public void redis3() throws JsonProcessingException {
           String s = mapper.writeValueAsString(car);
           stringRedisTemplate.opsForValue().set("liudie",s);
           /**得到数据*/
           String liudie = stringRedisTemplate.opsForValue().get("liudie");
           /**手动反序列化*/
           Car car = mapper.readValue(liudie, Car.class);
           System.out.println(car);
       }
       /**测试hash*/
       @GetMapping("/redis4")
       public void redis4(){
           stringRedisTemplate.opsForHash().put("name:123","name","刘碟");
           stringRedisTemplate.opsForHash().put("name:123","age","23");
           Map<Object, Object> entries = stringRedisTemplate.opsForHash().entries("name:123");
           Set<Object> objects = entries.keySet();
           for (Object object : objects) {
               System.out.println("KEY为:" +object +"Value为:"+entries.get(object));
           }
       }
       /**Rest风格传参
        * 获得请求头
        * http://localhost:8081/hello/pet/1/weight/22*/
       @GetMapping("/pet/{name}/weight/{weight}")
    public Map<String,Object> getPet(@PathVariable("name")  String name,
                                     @PathVariable("weight") String weight,
                                     @PathVariable Map<String,String> map,
                                     @RequestHeader Map<String,String> stringMap){
           HashMap<String, Object> stringObjectHashMap = new HashMap<>();
           stringObjectHashMap.put("name",name);
           stringObjectHashMap.put("weight",weight);
           stringObjectHashMap.put("map",map);
           stringObjectHashMap.put("Header",stringMap);
           System.out.println(stringMap);
           return stringObjectHashMap;
       }
       /**RestquestParams风格传参
        * http://localhost:8081/hello/params?id=1&name=%E5%88%98%E7%A2%9F
        * 得到Cookie最简单的方法就是从request中得到*/
       @GetMapping("/params")
       public Map<String,String> params(@RequestParam("id") String id,
                                        @RequestParam("name") String name,
                                        @CookieValue("password") Cookie cookie,
                                        HttpServletRequest request){
           HashMap<String, String> stringStringHashMap = new HashMap<>();
           stringStringHashMap.put("id",id);
           stringStringHashMap.put("name",name);
           System.out.println(cookie.getName()+"--->"+cookie.getValue());
           /**获取cookie*/
           Cookie[] cookies = request.getCookies();
           for (Cookie cookie1 : cookies) {
               System.out.println(cookie1.getName()+"-->"+cookie1.getValue());
           }
           String host = request.getParameter("id");
           System.out.println(host);
           String host1 = request.getHeader("Host");
           System.out.println(host1);
           return stringStringHashMap;
       }
       /**测试矩阵变量*/
       @GetMapping("/juzheng/{a}")
    public Map<String,Object> juzheng(@MatrixVariable(value = "name",pathVar = "a") String name,
                                      @MatrixVariable("id") Integer id,
                                      @MatrixVariable("a") String a){
           Map<String, Object> objectObjectHashMap = new HashMap<>();
         objectObjectHashMap.put("name",name);
         objectObjectHashMap.put("id",id);
         objectObjectHashMap.put("a",a);
         return  objectObjectHashMap;
       }
       /**测试参数处理具体位置*/
       @GetMapping("/param")
    public AjaxResult testParams(Map<String,Object> map,
                                 Model model,
                                 HttpServletRequest httpServletRequest,
                                 HttpServletResponse httpServletResponse){
           /**map与modely与attribute都是放在请求域中的值
            * cookie是自己添加到浏览器中响应的*/
           map.put("hello","world666");
           model.addAttribute("world","hello666");
//           httpServletRequest.setAttribute("token", UUID.randomUUID().toString().substring(0,9));
           httpServletRequest.setAttribute("message","helloWorld666");
           Cookie cookie = new Cookie("c1", "v1");
           cookie.setDomain("localhost");
           httpServletResponse.addCookie(cookie);
           return AjaxResult.success("请求成功");
       }

}
